//=========文件读写操作[结构体][ASCII码]======
//======可以尝试[二进制]的写法========

//======[问题一]:为什么重复执行程序,添加内容不重复[未解决]=====
#include <stdio.h>  
#include <stdlib.h>  

typedef struct 
{  
	char name[10];  
	int age;  
	char addr[20];  
}stu;

int main()  
{  
	FILE *fp1;
	FILE* fp2;
	FILE* fp3;
	//读文件
	if((fp1=fopen("data/test1.txt","r"))==NULL)  
	{  
		printf("不能打开文件\n");  											        exit(0);  
	}
	//写文件
	if((fp2=fopen("test_r.txt","w")) == NULL)
	{
		printf("不能打开文件\n");
		exit(0);
	}
	//添加文件
	if((fp3=fopen("test_r.txt","a")) == NULL)
	{
		printf("不能打开文件\n");
		exit(0);
	}
	printf("读取文件的内容如下:\n");  
	stu* st = (stu*)malloc(sizeof(stu));
	while(!feof(fp1))
	{
		fscanf(fp1,"%s%d%s\n",st->name,&st->age,st->addr);
		printf("%s %d %s\n",st->name, st->age+1, st->addr);  
		//fprintf(fp2,"%s %d %s\n",st->name,st->age,st->addr);
	}
	fprintf(fp3,"%s %d %s\n","Adder",22,"湖南省");
	fprintf(fp3,"%s %d %s\n","Adder",22,"湖南省");
	fprintf(fp3,"%s %d %s\n","Adder",22,"湖南省");
	free(st);
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	return 0; 
}  
